<template>
  <el-container class="home-container">
    <el-header>
      <div class="header-left">
        <h2>用户管理系统</h2>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleThemeChange" class="theme-dropdown">
          <span class="el-dropdown-link">
            <i :class="themeIcon"></i>
            更换主题
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="light">
              <i class="el-icon-sunny"></i>
              日间模式
            </el-dropdown-item>
            <el-dropdown-item command="dark">
              <i class="el-icon-moon"></i>
              夜间模式
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span>欢迎, {{ username }}</span>
        <el-button type="text" @click="logout">退出登录</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
          :default-active="activeIndex"
          class="el-menu-vertical"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          router>
          <el-menu-item index="/home/dashboard">
            <i class="el-icon-data-analysis"></i>
            <span slot="title">数据大屏</span>
          </el-menu-item>
          <el-menu-item index="/home/users">
            <i class="el-icon-user"></i>
            <span slot="title">用户管理</span>
          </el-menu-item>
          <el-menu-item index="/home/logs">
            <i class="el-icon-document"></i>
            <span slot="title">操作日志</span>
          </el-menu-item>
          <el-menu-item index="/home/blacklist">
            <i class="el-icon-lock"></i>
            <span slot="title">IP黑名单</span>
          </el-menu-item>
          <el-menu-item index="/home/recycle-bin">
            <i class="el-icon-delete"></i>
            <span slot="title">回收站</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
    <AIAssistant />
  </el-container>
</template>

<script>
import AIAssistant from '@/views/AIAssistant.vue'

export default {
  name: 'Home',
  components: {AIAssistant},
  data() {
    return {
      username: localStorage.getItem('username') || '未知用户',
      currentTheme: localStorage.getItem('theme') || 'light'
    }
  },
  computed: {
    activeIndex() {
      return this.$route.path
    },
    themeIcon() {
      return this.currentTheme === 'light' ? 'el-icon-sunny' : 'el-icon-moon'
    }
  },
  mounted() {
    this.applyTheme(this.currentTheme)
  },
  methods: {
    logout() {
      localStorage.removeItem('username')
      localStorage.removeItem('userId')
      localStorage.removeItem('userRole') // 清除角色信息
      this.$message.success('已退出登录')
      this.$router.push('/login')
    },
    handleThemeChange(theme) {
      this.currentTheme = theme
      localStorage.setItem('theme', theme)
      this.applyTheme(theme)
      this.$message.success(`已切换到${theme === 'light' ? '日间' : '夜间'}模式`)
    },
    applyTheme(theme) {
      const root = document.documentElement
      if (theme === 'dark') {
        root.classList.add('dark-theme')
      } else {
        root.classList.remove('dark-theme')
      }
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #409EFF;
  color: white;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
}
.header-left, .header-right {
  display: flex;
  align-items: center;
}
.header-right {
  color: white;
}
.header-right span {
  margin-right: 15px;
}
.el-aside {
  background-color: #545c64;
  color: white;
}
.el-menu {
  border-right: none;
}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
.theme-dropdown {
  margin-right: 20px;
  cursor: pointer;
}

.el-dropdown-link {
  color: white;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.el-dropdown-link:hover {
  color: #f0f0f0;
}
</style>